<!DOCTYPE html>
<html lang="zh"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/font.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="ebe-body">
    <div class="layui-fluid" id="ebe-systemUser">
        <div class="layui-card">
            <div class="layui-card-body ebe-table-full">
                <form class="layui-form ebe-search">
                    <div class="layui-row">
                        <div class="layui-col-md10">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">用户名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="username" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">创建时间</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="createTimeRange" id="createTime" autocomplete="off"
                                               class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">手机</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="mobile" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">姓名</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="trueName" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">性别</label>
                                    <div class="layui-input-inline">
                                        <select name="sex">
                                            <option value=""></option>
                                            <option value="1">男</option>
                                            <option value="2">女</option>
                                            <option value="0">保密</option>
                                        </select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label layui-form-label-sm">状态</label>
                                    <div class="layui-input-inline">
                                        <select name="status">
                                            <option value=""></option>
                                            <option value="0">禁用</option>
                                            <option value="1">有效</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md2 layui-col-sm12 layui-col-xs12  table-action-area">
                            <div class="layui-btn layui-btn-sm layui-btn-primary table-action" lay-submit
                                 lay-filter="data-search-btn" id="query">
                                <i class="layui-icon layui-icon-search"></i>
                            </div>
                            <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                                 shiro:hasAnyPermissions="user:add,user:update,user:password:reset">
                                <i class="layui-icon layui-icon-unorderedlist"></i>
                            </div>
                        </div>
                    </div>
                </form>
                <table class="layui-hide" id="userTable" lay-filter="currentTableFilter"></table>
                <script type="text/html" id="currentTableBar">
                    <span shiro:lacksPermission="user:view,user:update,user:delete">
        				<span class="layui-badge-dot"></span> 无权限
    				</span>
                    <a  shiro:hasPermission="user:view" class="layui-btn layui-btn-xs layui-btn-blue" lay-event="detail">查看</a>
                    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"
                       shiro:hasPermission="user:update">编辑</a>
                    <a class="layui-btn layui-btn-xs layui-btn-red data-count-delete" lay-event="delete"
                       shiro:hasPermission="user:delete">删除</a>
                </script>
            </div>
        </div>
    </div>
</div>
<script src="/lib/layui/layui.js" charset="utf-8"></script>
<script src="/lib/lay-config.js" charset="utf-8"></script>
<script type="text/html" id="user-sex">
    {{#
    var sex = {
    0: {title: '保密'},
    1: {title: '男'},
    2: {title: '女'}
    }[d.sex];
    }}
    <span>{{ sex.title }}</span>
</script>
<script type="text/html" id="user-status">
    {{#
    var status = {
    1: {title: '有效', color: 'green'},
    0: {title: '禁用', color: 'red'}
    }[d.status];
    }}
    <span class="ebe-badge ebe-tag-{{status.color}}">{{ status.title }}</span>
</script>
<script data-th-inline="none">
    layui.use(['form', 'table', 'layuimini', 'dropdown', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layuimini = layui.layuimini,
            dropdown = layui.dropdown,
            laydate = layui.laydate,
            table = layui.table;
        // 时间组件
        laydate.render({
            elem: '#createTime',
            range: true,
            trigger: 'click'
        });
        //表格
        layuimini.table.init({
            elem: '#userTable',
            url: '/system-user/list',
            cols: [[
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'systemUserId', title: 'ID', type: 'numbers'},
                {field: 'username', title: '用户名'},
                {field: 'trueName', title: '姓名'},
                {title: '性别', templet: "#user-sex"},
                {field: 'mobile', title: '手机'},
                {title: '状态', templet: "#user-status"},
                {field: 'createTime', title: '创建时间', minWidth: 180, sort: true, align: "center"},
                {title: '操作', minWidth: 180, toolbar: '#currentTableBar', fixed: "right", align: "center"}
            ]]
        });
        dropdown.render({
            elem: $('.action-more'),
            click: function (name, elem, event) {
                var checkStatus = table.checkStatus('userTable');
                let checkLength = checkStatus.data.length;
                if (name === 'add') {
                    layuimini.model.open('新增用户', '/system/user/add', {
                        btn: ['提交', '重置'],
                        area: $(window).width() <= 750 ? '95%' : '50%',
                        yes: function (index, layero) {
                            $('#user-add').find('#submit').trigger('click');
                        },
                        btn2: function () {
                            $('#user-add').find('#reset').trigger('click');
                            return false;
                        }
                    });
                }
                if (name === 'delete') {
                    if (!checkLength) {
                        layuimini.alert.warn('请选择需要删除的用户');
                    } else {
                        layuimini.modal.confirm('删除用户', '确定删除所选用户？', function () {
                            var userIds = [];
                            layui.each(checkStatus.data, function (key, item) {
                                userIds.push(item.systemUserId)
                            });
                            layuimini.delete('/system-user/' + userIds.join(','), function () {
                                table.reload('userTable', {
                                    page: {
                                        curr: layuimini.judgeRemoveTablePage('userTable',checkLength)
                                    }
                                }, 'data');
                                layuimini.alert.success('删除用户成功');
                            });
                        });
                    }
                }
                if (name === 'reset') {
                    if (!checkLength) {
                        layuimini.alert.warn('请选择需要重置密码的用户');
                    } else {
                        layuimini.modal.confirm('重置密码', '确定重置密码？', function () {
                            var usernames = [];
                            layui.each(checkStatus.data, function (key, item) {
                                usernames.push(item.username)
                            });
                            layuimini.put('/system-user/password/reset/' + usernames.join(','), null, function () {
                                layuimini.alert.success('所选用户密码已重置为qweasd');
                            });
                        });
                    }
                }

            },
            options: [{
                name: 'add',
                title: '新增用户',
                perms: 'user:add'
            }, {
                name: 'delete',
                title: '删除用户',
                perms: 'user:delete'
            }, {
                name: 'reset',
                title: '密码重置',
                perms: 'user:password:reset'
            }]
        });
        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let result = data.field;
            //执行搜索重载
            table.reload('userTable', {
                page: {
                    curr: 1
                },
                where: result

            }, 'data');

            return false;
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'detail') {
                layuimini.model.view('用户信息', '/system/user/userInfo/' + data.systemUserId, {
                    area: $(window).width() <= 750 ? '95%' : '660px'
                });
            }
            if (layEvent === 'delete') {
                layuimini.modal.confirm('删除用户', '确定删除该用户？', function () {
                    layuimini.delete('/system-user/' + data.systemUserId, function () {

                        table.reload('userTable', {
                            page: {
                                curr: layuimini.judgeRemoveTablePage('userTable',1)
                            }
                        }, 'data');
                        layuimini.alert.success('删除用户成功');
                    });
                });
            }
            if (layEvent === 'edit') {
                layuimini.model.open('修改用户', '/system/user/update/' + data.systemUserId, {
                    area: $(window).width() <= 750 ? ['90%'] : ['50%'],
                    btn: ['提交', '取消'],
                    yes: function (index, layero) {
                        $('#user-update').find('#submit').trigger('click');
                    },
                    btn2: function () {
                        layer.closeAll();
                    }
                });
            }
        });

    });
</script>
</body>
</html>
